﻿using System;
using System.Web.Http;
using System.Collections.Specialized;
using PreeRegisterBLL;
using PreeRegisterModel;
using PreeRegisterBLL.Security;
using Newtonsoft.Json.Linq;

namespace PreeRegister_WebAPI.Controllers
{
    public class workController : ApiController
    {
        [HttpPost]
        /// <summary>
        /// 排班&号源更新
        /// http://172.30.0.131:10018/api/work/update
        /// http://192.168.0.103:8080/api/work/update
        /// jsonData={"head":{"fromtype":"UH_RDSP_ISO","sessionid":"10","time":"1432603753540","token":"f6f0020aadc680a62a0550e8800482ae","version":"1"},"body":{"optype":"2","hospid":"66","ids":"213700,213699"}}
        /// </summary>
        /// <param name="any"></param>
        /// <returns></returns>
        public EncodeModel update(NameValueCollection any)
        {
            try
            {
                //获取JSON加密参数
                string head_json = CommonBLL.RequestGetParam("head").ToString();
                string body_json = CommonBLL.RequestGetParam("body").ToString();

                //拼装jsonData类
                EncodeModel encodeModel = new EncodeModel();
                encodeModel.head = head_json;
                encodeModel.body = body_json;

                //转化jsonData串
                string jsonData = csSerialize.ResponseInfo(encodeModel);

                //解密头和内容信息
                string ErrMsg = "";
                string ReturnBodyJson = "";
                if (!ResultDecode.DecodeResult(jsonData, "", ref head_json, ref body_json, ref ErrMsg))
                {
                    WorkUpdateBodyModel workUpdateBodyModel = new WorkUpdateBodyModel();

                    //序列化返回内容类
                    ReturnBodyJson = csSerialize.WorkUpdateResponseBodyInfo(workUpdateBodyModel);

                    //加密返回内容Json
                    return ResultDecode.EncodeResult(ReturnBodyJson);
                }

                //入口参数日志
                CommonBLL.WriteLog("work|update|Post|\r\n" + body_json + "\r\n\r\n");

                //反序列化请求字符串
                WorkUpdateRequestModel workUpdateRequestModel = WorkBLL.ReturnWorkUpdateRequestModel(head_json, body_json);

                //获取排班号源
                WorkUpdateResponseModel workUpdateResponseModel = WorkBLL.WorkUpdate(workUpdateRequestModel);

                //序列化返回内容类
                ReturnBodyJson = csSerialize.WorkUpdateResponseBodyInfo(workUpdateResponseModel.body);

                //返回参数日志
                CommonBLL.WriteLog("work|update|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);

            }
            catch (Exception err)
            {
                WorkUpdateBodyModel workUpdateBodyModel = new WorkUpdateBodyModel();

                //序列化返回内容类
                string ReturnBodyJson = csSerialize.WorkUpdateResponseBodyInfo(workUpdateBodyModel);

                //返回参数日志
                CommonBLL.WriteLog("work|update|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
        }

        [HttpPost]
        /// <summary>
        /// 排班获取
        /// http://172.30.0.131:10018/api/work/update
        /// http://192.168.0.103:8080/api/work/update
        /// jsonData={"head":{"fromtype":"UH_RDSP_ISO","sessionid":"10","time":"1432603753540","token":"f6f0020aadc680a62a0550e8800482ae","version":"1"},"body":{"optype":"2","hospid":"66","ids":"213700,213699"}}
        /// </summary>
        /// <param name="any"></param>
        /// <returns></returns>
        public EncodeModel get(NameValueCollection any)
        {
            try
            {
                //获取JSON加密参数
                string jsonData = CommonBLL.RequestGetParam("jsonData").ToString();

                WorkGetResponseBodyModel workGetResponseBodyModel = new WorkGetResponseBodyModel();

                //解密头和内容信息
                string head_json = "";
                string body_json = "";
                string ErrMsg = "";
                string ReturnBodyJson = "";
                if (!ResultDecode.DecodeResult(jsonData, "work|get", ref head_json, ref body_json, ref ErrMsg))
                {

                    //序列化返回内容类
                    ReturnBodyJson = csSerialize.WorkGetResponseBodyModel(workGetResponseBodyModel);

                    //加密返回内容Json
                    return ResultDecode.EncodeResult(ReturnBodyJson);
                }

                //入口参数日志
                CommonBLL.WriteLog("work|get|Post|\r\n" + body_json + "\r\n\r\n");

                //反序列化请求字符串
                WorkGetRequestBodyModel workGetRequestBodyModel = csSerialize.WorkGetRequestBodyModel(body_json);

                //获取排班号源
                workGetResponseBodyModel = WorkGetBLL.GetWork(workGetRequestBodyModel);

                //序列化返回内容类
                ReturnBodyJson = csSerialize.WorkGetResponseBodyModel(workGetResponseBodyModel);

                //返回参数日志
                CommonBLL.WriteLog("work|get|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);

            }
            catch (Exception err)
            {
                WorkGetResponseBodyModel workGetResponseBodyModel = new WorkGetResponseBodyModel();

                //序列化返回内容类
                string ReturnBodyJson = csSerialize.WorkGetResponseBodyModel(workGetResponseBodyModel);

                //返回参数日志
                CommonBLL.WriteLog("work|get|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
        }

        [HttpPost]
        /// <summary>
        /// 号源获取
        /// http://172.30.0.131:10018/api/work/update
        /// http://192.168.0.103:8080/api/work/update
        /// jsonData={"head":{"fromtype":"UH_RDSP_ISO","sessionid":"10","time":"1432603753540","token":"f6f0020aadc680a62a0550e8800482ae","version":"1"},"body":{"optype":"2","hospid":"66","ids":"213700,213699"}}
        /// </summary>
        /// <param name="any"></param>
        /// <returns></returns>
        public EncodeModel detail(NameValueCollection any)
        {
            try
            {
                //获取JSON加密参数
                string jsonData = CommonBLL.RequestGetParam("jsonData").ToString();

                WorkSourceResponseModel workSourceResponseModel = new WorkSourceResponseModel();

                //解密头和内容信息
                string head_json = "";
                string body_json = "";
                string ErrMsg = "";
                string ReturnBodyJson = "";
                if (!ResultDecode.DecodeResult(jsonData, "work|detail", ref head_json, ref body_json, ref ErrMsg))
                {

                    //序列化返回内容类
                    ReturnBodyJson = csSerialize.WorkSourceResponseModel(workSourceResponseModel);

                    //加密返回内容Json
                    return ResultDecode.EncodeResult(ReturnBodyJson);
                }

                //入口参数日志
                CommonBLL.WriteLog("work|detail|Post|\r\n" + body_json + "\r\n\r\n");

                //反序列化请求字符串
                WorkSourceRequestModel workSourceRequestModel = csSerialize.WorkSourceRequestModel(body_json);

                //获取号源
                workSourceResponseModel = WorkGetBLL.GetWorkSource(workSourceRequestModel);

                //序列化返回内容类
                ReturnBodyJson = csSerialize.WorkSourceResponseModel(workSourceResponseModel);

                //返回参数日志
                CommonBLL.WriteLog("work|detail|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);

            }
            catch (Exception err)
            {
                WorkSourceResponseModel workSourceResponseModel = new WorkSourceResponseModel();

                //序列化返回内容类
                string ReturnBodyJson = csSerialize.WorkSourceResponseModel(workSourceResponseModel);

                //返回参数日志
                CommonBLL.WriteLog("work|detail|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
        }

        [HttpPost]
        public EncodeModel getjson(NameValueCollection any)
        {
            try
            {
                //获取JSON加密参数
                string head_json = CommonBLL.RequestGetParam("head").ToString();
                string body_json = CommonBLL.RequestGetParam("body").ToString();

                //拼装jsonData类
                EncodeModel encodeModel = new EncodeModel();
                encodeModel.head = head_json;
                encodeModel.body = body_json;

                //转化jsonData串
                string jsonData = csSerialize.ResponseInfo(encodeModel);

                //解密头和内容信息
                string ErrMsg = "";
                string ReturnBodyJson = "";
                if (!ResultDecode.DecodeResult(jsonData, "", ref head_json, ref body_json, ref ErrMsg))
                {
                    WorkUpdateBodyModel workUpdateBodyModel = new WorkUpdateBodyModel();

                    //序列化返回内容类
                    ReturnBodyJson = csSerialize.WorkUpdateResponseBodyInfo(workUpdateBodyModel);

                    //加密返回内容Json
                    return ResultDecode.EncodeResult(ReturnBodyJson);
                }

                //入口参数日志
                CommonBLL.WriteLog("work|getjson|Post|\r\n" + body_json + "\r\n\r\n");

                //反序列化请求字符串
                WorkUpdateRequestModel workUpdateRequestModel = WorkBLL.ReturnWorkUpdateRequestModel(head_json, body_json);

                int synday = 9;
                int.TryParse(workUpdateRequestModel.body.synday, out synday);
                //获取排班号源
                string json = WorkBLL.GetWorkJson(workUpdateRequestModel.body.docid, workUpdateRequestModel.body.dates, synday);


                //返回参数日志
                CommonBLL.WriteLog("work|getjson|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                //加密返回内容Json
                return ResultDecode.EncodeResult2(json);
            }
            catch (Exception err)
            {
                WorkUpdateBodyModel workUpdateBodyModel = new WorkUpdateBodyModel();

                //序列化返回内容类
                string ReturnBodyJson = csSerialize.WorkUpdateResponseBodyInfo(workUpdateBodyModel);

                //返回参数日志
                CommonBLL.WriteLog("work|getjson|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
        }
    }
}
